#!/usr/bin/python3
# -*- coding: utf-8 -*-

import sys
from datetime import datetime
import json
import random
import time

from com.utils import dbutil
from com.lechebang.spider.autohome import Dealer, City, DealerLocation
from com.lechebang.spider.autohome import BrandAndCarType

#data: data arrays
#table: mysql table name


def crawl_deaer():
    con=dbutil.connectDB(mysqlDB)
    # 清除老数据
    backupTable = 'crawler_dealer_%s' % datetime.now().strftime('%Y%m%d')
    dbutil.backupMysql(con, mysqlDB, dealerTable, backupTable);
    Dealer.clearDealer(dealerTable,con)
    print('====>北方mysq数据，%s ---->' % (dealerTable,backupTable))

    # 获取所有城市
    cities = City.queryCitiesFromDB(cityTable)
    for city in cities[1:]:
        page = 1
        dealers = Dealer.get_dealer(city['pinyin'], page)

        # 保持数据
        file = open('./data/' + city['pinyin'] + datetime.now().strftime('%Y-%m-%d') + '.json', 'w')
        json.dump(dealers, file)
        print('==========>save data to' + file.name)
        Dealer.saveDealer(dealers, dealerTable)
        time.sleep(random.uniform(1, 10))  # 随机休息几秒

 #deaerId #从哪个dealer开始，方便中断后继续
def crawl_brand(dealerId=1):
    dealers=Dealer.queryDealersFromDB(mysqlDB,dealerTable)
    #拉取品牌
    for dealer in dealers:
        if dealer['id']<dealerId:
            continue
        hompageUrl=dealer['homepage']
        if (hompageUrl is None or len(hompageUrl)<=0):
            continue
        url=dealer['homepage'].split('#')[0][0:-1]
        brand=BrandAndCarType.get_brandAndCarSeries(url)
        if brand is None:
            continue
        BrandAndCarType.saveBrand(dealer['id'],brand,brandTable)
        time.sleep(random.uniform(1, 10))  # 随机休息几秒

if __name__ == '__main__':
    mysqlDB='lcb_mars_db'
    cityTable='crawler_city'
    dealerTable='crawler_dealer'
    # dealerTable='shop_store_withbrand_center'
    brandTable='crawler_brand'
    # firstCity='tianjin'  #从哪个城市开始计算
    lcbDB='lcb_db'

    para = sys.argv[1]  ## 外部参数，数据库
    if para=='dealer':
        print('dealer')
        crawl_deaer()
    elif para=='brand':
        crawl_brand(1)
    elif para=='location':
        DealerLocation.uploadLocation(lcbDB,dealerTable,1)
    elif para=='distance':
        DealerLocation.updateDistance(lcbDB,'lcb_db.shop_store_withbrand_center',1)
    else:
        print('error paras')

